/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "cmd.h"
#include "example_task.h"
#include "Motor.h"
#include "rc_sbus.h"
#include "Servo.h"
#include "transmission.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
extern int8_t Start_Flag ;
extern int8_t Back_Flag ;//由cmd的SHOOT给出
extern int8_t Move_Back_Flag ; //给回cmd倒退
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

 uint16_t Test_NoInit1 /*__attribute__( ( section( ".noinit")) )*/ ;
/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for TestTask */
osThreadId_t TestTaskHandle;
const osThreadAttr_t TestTask_attributes = {
  .name = "TestTask",
  .stack_size = 1024 * 4,
  .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for Transmission */
osThreadId_t TransmissionHandle;
const osThreadAttr_t Transmission_attributes = {
  .name = "Transmission",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for CMD_Mastar */
osThreadId_t CMD_MastarHandle;
const osThreadAttr_t CMD_Mastar_attributes = {
  .name = "CMD_Mastar",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for CMD_Slave */
osThreadId_t CMD_SlaveHandle;
const osThreadAttr_t CMD_Slave_attributes = {
  .name = "CMD_Slave",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityLow,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);
void StartTask02(void *argument);
void Transmission_Entry(void *argument);
void CMD_Mastar_Entry(void *argument);
void CMD_Slave_Entry(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */
  TransmissionUart_Init();
  Servo_Init();
  Motor_Init();
  example_init();
  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* creation of TestTask */
  TestTaskHandle = osThreadNew(StartTask02, NULL, &TestTask_attributes);

  /* creation of Transmission */
  TransmissionHandle = osThreadNew(Transmission_Entry, NULL, &Transmission_attributes);

  /* creation of CMD_Mastar */
  CMD_MastarHandle = osThreadNew(CMD_Mastar_Entry, NULL, &CMD_Mastar_attributes);

  /* creation of CMD_Slave */
  CMD_SlaveHandle = osThreadNew(CMD_Slave_Entry, NULL, &CMD_Slave_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
    // if(Test_Init == 1)
      // Test_NoInit1 += 10;
    osDelay(100);
  }
  /* USER CODE END StartDefaultTask */
}

/* USER CODE BEGIN Header_StartTask02 */
/**
* @brief Function implementing the TestTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask02 */
void StartTask02(void *argument)
{
  /* USER CODE BEGIN StartTask02 */
  /* Infinite loop */
  for(;;)
  {
    example_thread_entry();
    osDelay(5);
  }
  /* USER CODE END StartTask02 */
}

/* USER CODE BEGIN Header_Transmission_Entry */
/**
* @brief Function implementing the Transmission thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Transmission_Entry */
void Transmission_Entry(void *argument)
{
  /* USER CODE BEGIN Transmission_Entry */
  /* Infinite loop */
  for(;;)
  {
    Check_UART_Transmission();
    osDelay(1);
  }
  /* USER CODE END Transmission_Entry */
}

/* USER CODE BEGIN Header_CMD_Mastar_Entry */
/**
* @brief Function implementing the CMD_Mastar thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_CMD_Mastar_Entry */
void CMD_Mastar_Entry(void *argument)
{
  /* USER CODE BEGIN CMD_Mastar_Entry */
  /* Infinite loop */
  for(;;)
  {
    if(Start_Flag == 0)
    {
      Start_Flag = 1;
      osDelay(2000);
      Start_Flag = 2;
    }
    if(Back_Flag == 1)
    {
      Move_Back_Flag = 1;
      osDelay(2000);
      Back_Flag = 0;
      Move_Back_Flag = 0;
    }
    osDelay(1);
  }
  /* USER CODE END CMD_Mastar_Entry */
}

/* USER CODE BEGIN Header_CMD_Slave_Entry */
/**
* @brief Function implementing the CMD_Slave thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_CMD_Slave_Entry */
void CMD_Slave_Entry(void *argument)
{
  /* USER CODE BEGIN CMD_Slave_Entry */
  /* Infinite loop */
  for(;;)
  {
    Car_Action();
    osDelay(10);
  }
  /* USER CODE END CMD_Slave_Entry */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

